//
//  RWViewController.m
//  RWReactivePlayground
//
//  Created by Colin Eberhardt on 18/12/2013.
//  Copyright (c) 2013 Colin Eberhardt. All rights reserved.
//

#import "RWViewController.h"
#import "RWDummySignInService.h"
#import <ReactiveCocoa/ReactiveCocoa.h>
#import <ReactiveCocoa/ReactiveCocoa.h>
@interface RWViewController ()

@property (weak, nonatomic) IBOutlet UITextField *usernameTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;
@property (weak, nonatomic) IBOutlet UIButton *signInButton;
@property (weak, nonatomic) IBOutlet UILabel *signInFailureText;

@property (strong, nonatomic) RWDummySignInService *signInService;

@end

@implementation RWViewController

- (void)viewDidLoad {
  [super viewDidLoad];
    self.signInService = [RWDummySignInService new];
    
    
    self.signInFailureText.hidden = YES;
    
    RACSignal* validUserNameSignal = [self.usernameTextField.rac_textSignal map:^id(NSString* text) {
      
        return @([self isValidUsername:text]);
    }];
    
    RACSignal* validPassWordSignal = [self.passwordTextField.rac_textSignal map:^id(NSString* text) {
        
        return @([self isValidPassword:text]);
    }];
    
    RAC(self.usernameTextField,backgroundColor) = [validUserNameSignal map:^id(NSNumber* userNameValid) {
        
        return [userNameValid boolValue]?[UIColor clearColor]:[UIColor orangeColor];
    }];
    
    
    RAC(self.passwordTextField,backgroundColor) = [validPassWordSignal map:^id(NSNumber* passwordValid) {
       
        return [passwordValid boolValue]?[UIColor clearColor]:[UIColor orangeColor];
    }];
    
 
    
    RACSignal* signUpActiveSignal = [[RACSignal combineLatest:@[validUserNameSignal,validPassWordSignal]]reduceEach:^id(NSNumber* userNameValid,NSNumber* passwordValid){
        
        return @([userNameValid boolValue]&&[passwordValid boolValue]);
        
    }];
    
    [signUpActiveSignal subscribeNext:^(NSNumber *signupActive) {
        self.signInButton.enabled = [signupActive boolValue];
    }];
    
    [[[[self.signInButton rac_signalForControlEvents:UIControlEventTouchUpInside]doNext:^(id x) {
        self.signInButton.enabled = NO;
        self.signInFailureText.hidden = YES;
    }] flattenMap:^RACStream *(id value) {
        return self.signInSignal;
    }] subscribeNext:^(NSNumber *signedIn) {
        self.signInButton.enabled = YES;
        BOOL success = signedIn.boolValue;
        self.signInFailureText.hidden = success;
        if (success) {
            [self performSegueWithIdentifier:@"signInSuccess" sender:self];
        }
    }];
    
    
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self.view endEditing:YES];
}
- (BOOL)isValidUsername:(NSString *)username {
    return username.length > 3;
}

- (BOOL)isValidPassword:(NSString *)password {
    return password.length > 3;
}

-(RACSignal *)signInSignal {
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        [self.signInService
         signInWithUsername:self.usernameTextField.text
         password:self.passwordTextField.text
         complete:^(BOOL success) {
             [subscriber sendNext:@(success)];
             [subscriber sendCompleted];
         }];
        return nil;
    }];
}


@end
